package com.rms.hkpc.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.rms.hkpc.R;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.util.DensityUtil;
import com.rms.hkpc.util.ImageUtil;
import com.rms.hkpc.util.OKHttpUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    private SparseArray<View> mViews;
    private View convertView;

    public BaseViewHolder(Context context,View itemView) {
        super(itemView);
        this.context = context;
        convertView = itemView;
        mViews = new SparseArray<>();
    }

    public static BaseViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(context,itemView);
    }

    public <T extends View> T fd(int id) {
        View view = mViews.get(id);
        if (view == null) {
            view = convertView.findViewById(id);
            mViews.put(id, view);
        }
        return (T) view;
    }

    public void setImageResource(Activity activity, int id, String resource, View.OnClickListener onClickListener) {
        ImageView iv = fd(id);
        ImageUtil.getInstance().loadImg(activity,resource,iv);
        iv.setVisibility(View.VISIBLE);
        iv.setOnClickListener(onClickListener);
    }

    public void setVisibility(int id, int visible) {
        View v = fd(id);
        v.setVisibility(visible);
        v.setOnClickListener(null);
    }

    public void setText(int id, String text) {
        TextView tv = fd(id);
        tv.setText(text);
    }

    public void setTextAndColor(int id, String text, String color) {
        TextView tv = fd(id);
        tv.setText(text);
        tv.setTextColor(Color.parseColor(color));
    }

    public void setInsight(Integer num, String color) {
        TextView view = fd(R.id.tv_num);
        if (num == null) {
            view.setText("-");
        }else {
            view.setText(String.valueOf(num));
        }
        GradientDrawable gd = (GradientDrawable) view.getBackground();
        try {
            view.setTextColor(Color.parseColor(color));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor(color));
        } catch (Exception e) {
            e.printStackTrace();
            view.setTextColor(Color.parseColor("#ff0000"));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor("#ff0000"));
        }
    }

    public void setInsightPercentage(float num, String color) {
        TextView view = fd(R.id.tv_num);
        BigDecimal b = new BigDecimal(String.valueOf(num));
        BigDecimal c = new BigDecimal(String.valueOf(100));
        view.setText(b.multiply(c).doubleValue() + "%");
        GradientDrawable gd = (GradientDrawable) view.getBackground();
        try {
            view.setTextColor(Color.parseColor(color));
            gd.setColor(context.getResources().getColor(R.color.color_f2));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor(color));
        } catch (Exception e) {
            e.printStackTrace();
            view.setTextColor(Color.parseColor("#ff0000"));
            gd.setStroke(DensityUtil.dp2px(4), Color.parseColor("#ff0000"));
        }
    }

    public void setInsight(String color) {
        TextView view = fd(R.id.tv_result);
        GradientDrawable gd = new GradientDrawable();
        gd.setShape(GradientDrawable.OVAL);
        try {
            int parseColor = Color.parseColor(color);
            gd.setColor(parseColor);
            gd.setStroke(DensityUtil.dp2px(2),getDarkerColor(parseColor));
        } catch (Exception e) {
            e.printStackTrace();
            gd.setColor(Color.parseColor("#DA0202"));
            gd.setStroke(DensityUtil.dp2px(2), Color.parseColor("#ff0000"));
        }
        view.setBackground(gd);
    }

    /**
     * 将颜色转换成深色
     * @param color
     * @return
     */
    public int getDarkerColor(int color){
        float[] hsv = new float[3];
        Color.colorToHSV(color,hsv);
        hsv[1] = hsv[1] + 0.1f;
        hsv[2] = hsv[2] - 0.1f;
        return Color.HSVToColor(hsv);
    }
}
